<template>
  <div class="addDep">
    <div class="header">
      <h1>部门管理</h1>
      <div class="header-right">
        <i class="el-icon-s-home"></i>
        <span class="font-left">首页</span>
        <i class="el-icon-arrow-right"></i>
        <span class="font-right">部门管理</span>
      </div>
    </div>
    <div class="content">
      <div class="bar">
        <el-button
          type="info"
          icon="el-icon-arrow-left"
          size="mini"
          @click="goBack"
        >
          返回
        </el-button>
      </div>
      <el-form
        size="small"
        class="addForm"
        ref="addForm"
        :model="addForm"
        :rules="rules"
      >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item prop="depName">
              <span slot="label">
                <span class="labelText">名称</span>
              </span>
              <el-input
                v-model="addForm.depName"
                :validate-event="false"
              ></el-input>
            </el-form-item>
            <el-form-item prop="fax">
              <span slot="label">
                <span class="labelText">传真</span>
              </span>
              <el-input
                v-model="addForm.fax"
                :validate-event="false"
              ></el-input>
            </el-form-item>
            <el-form-item prop="depAddr">
              <span slot="label">
                <span class="labelText">地址</span>
              </span>
              <el-input
                v-model="addForm.depAddr"
                :validate-event="false"
              ></el-input>
            </el-form-item>
            
          </el-col>
          <el-col :span="12">
            <el-form-item prop="depPho">
              <span slot="label">
                <span class="labelText">电话</span>
              </span>
              <el-input
                v-model="addForm.depPho"
                :validate-event="false"
              ></el-input>
            </el-form-item>
            <el-form-item prop="email">
              <span slot="label">
                <span class="labelText">邮箱</span>
              </span>
              <el-input
                v-model="addForm.email"
                :validate-event="false"
              ></el-input>
            </el-form-item>
            <el-form-item prop="workTime">
              <span slot="label">
                <span class="labelText">上下班时间</span>
                <span class="labelText2">如08:00-12:00|14:00-18:00</span>
              </span>
              <el-input
                v-model="addForm.workTime"
                :validate-event="false"
              ></el-input>
            </el-form-item>
            <!-- <el-form-item prop="sort">
              <span slot="label">
                <span class="labelText">排序</span>
              </span>
              <el-input
                v-model="addForm.sort"
                :validate-event="false"
              ></el-input>
            </el-form-item> -->
          </el-col>
        </el-row>
        <el-form-item class="btnBox">
          <el-button type="primary" size="small" @click="($event) => addDep('addForm')"
            >保存</el-button
          >
          <el-button type="info" size="small" @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import dayjs from "dayjs";
export default {
  data() {
    return {
      depList: [],
      addForm: {
        depName: "",
        fax: "",
        depAddr: "",
        workTime: "",
        depPho: "",
        email: "",
        sort: "1",
        pid: "",
      },
      rules: {
        depName: [
          { required: true, message: "请输入名称", trigger: "blur" },
          {
            min: 2,
            max: 10,
            message: "请保持在2-10个字符之间！",
            trigger: "blur",
          },
        ],
        fax: [
          { min: 5, max: 20, message: "字符字数不合规定！", trigger: "blur" },
        ],
      },
    };
  },

  mounted() {},

  created() {
    this.addForm.pid = this.$route.params.depId;
    console.log(this.$route.params.depId);
    console.log(this.addForm.pid);
  },
  methods: {
    goBack() {
      this.$router.push("/user/depManage");
    },
    //增加部门
    addDep(addForm) {
      this.$refs[addForm].validate((valid) => {
        if (valid){
          this.addForm.workTime = new Date(
          dayjs().format("YYYY-MM-DD HH:mm:ss")
        ).toISOString();
        this.$api.user.addDep(this.addForm).then((res) => {
          if (res.data.code === 200) {
            //跳转到列表页面
            this.$router.push("/user/depManage");
            //提示
            this.$message({
              message: "添加部门成功",
              type: "success",
            });
            console.log(res);
          } else {
            //错误信息提示
            this.$message.error("出现错误，添加部门失败");
            console.log(res);
          }
        });
        }else{
          this.$message.error( '请填写必填项')
        }
        
      });
    },
    //取消按钮
    cancel() {
      this.$router.push("/user/depManage");
    },
  },
};
</script>

<style lang="less" scoped>
html body {
  height: 100%;
  background-color: #f5f5f5;
}
.addDep {
  height: 100%;
}
.header {
  position: relative;
  padding: 15px 15px 0 15px;
  display: flex;
  justify-content: space-between;
}
.header > h1 {
  margin: 0;
  font-size: 24px;
  font-weight: 500;
}
.header-right {
  font-size: 12px;
  display: flex;
  align-items: center;
}
.el-icon-arrow-right {
  padding: 0 5px;
  color: #ccc;
}
.el-icon-s-home {
  color: #444;
  display: inline-block;
  margin-right: 5px;
}
.font-left {
  color: #444;
}
.font-right {
  color: #777;
}
.content {
  padding: 15px;
}
.bar {
  display: flex;
  justify-content: space-between;
  background-color: #fff;
  padding: 10px;
  border-top: 3px solid #d2d6de;
  border-bottom: 1px solid #f4f4f4;
}
.labelText {
  color: #333333;
  font-size: 14px;
}
.labelText2 {
  color: #999999;
  font-size: 12px;
  margin-left: 5px;
}
.addForm {
  background-color: #fff;
  padding: 5px;
}
</style>
